//
//  FeedbackVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/30.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class FeedbackVC: BaseVC {

  fileprivate var contentV: UIView!
  
  fileprivate var textV: RTRootTextView!
  
  fileprivate var submitButton: ObserveButton!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = GlobalConfig.BGWhiteColor
    title = "意见与反馈"
    
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    IQKeyboardManager.sharedManager().enable = false
    
    GlobalConfig.baiduStatisticsPageViewStart("意见反馈")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    IQKeyboardManager.sharedManager().enable = true
    
    GlobalConfig.baiduStatisticsPageViewEnd("意见反馈")
  }

}

// MARK: - action

extension FeedbackVC {
  
  @objc fileprivate func submitAction() {
    
    textV.resignFirstResponder()
    
    SJApp.showHUD()
    let _ = WorkorderRequest.shared.feedback(content: textV.text) {
      SJToast.show("感谢您的反馈!")
      self.navigationController?.popViewController(animated: true)
    }
  
  }

}

// MARK: - UITextViewDelegate

extension FeedbackVC: RootTextViewDelegate {

    func textWillChange(_ inputView: RTRootTextView!) {
        submitButton.isSelected = !inputView.text.isEmpty
    }
  
}

// MARK: - fileprivate

extension FeedbackVC {
  
  fileprivate func create() {
    
    addBackItem()
    
    submitButton = ObserveButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
    submitButton.setTitle("提交", for: .normal)
    submitButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
    submitButton.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
    submitButton.isSelected = false
    let rightItem = UIBarButtonItem(customView: submitButton)
    navigationItem.rightBarButtonItem = rightItem
    
    contentV = UIView()
    view.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.top.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(280.adaptor)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    textV = RTRootTextView(frame:CGRect(x: 15,y: 15,width: UIScreen.width-30, height:280))
    contentV.addSubview(textV)
    textV.font = GlobalConfig.system16Font
    textV.textColor = GlobalConfig.TextDeepColor
    textV.customDelegate = self
    textV.placeholder = "我们希望收到您宝贵的建议"
    
    
    textV.becomeFirstResponder()

  }
  
}
